/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.cid.beans;

import edu.chl.cid.entities.Product;
import java.beans.*;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author rhodinj
 */
@Stateless
public class ProductEJB {

    @PersistenceContext(unitName="sasanshop_pu")
    private EntityManager em;


    public List<Product> findProducts() {

        //TypedQuery<Product> query = em.createNamedQuery("findAllProducts", Product.class);
        //return query.getResultList();
        Query query = em.createNamedQuery("findAllProducts");
        return query.getResultList();
    }

    public Product getProductByName(String name) {
        Query query = em.createNamedQuery("SELECT p FROM Product p WHERE p.name ==" + name);
        return (Product) query.getSingleResult();
    }

    public Product createProduct(Product product) {
        em.persist(product);
        return product;
    }



}


